package com.tcoiss.app.controller;

import com.tcoiss.app.domain.ElectronicFence;
import com.tcoiss.app.service.IFencePointsService;
import com.tcoiss.app.utils.SplitList;
import com.tcoiss.common.core.enums.ErroEnums;
import com.tcoiss.common.core.web.controller.BaseController;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.log.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/gaode")
public class GaodeController extends BaseController {

    private final IFencePointsService iFencePointsService;

    //@PreAuthorize(hasPermi = "${integration:gaode}:list")
    @GetMapping("/queryFence")
    public AjaxResult queryPsFence(ElectronicFence electronicFence) {
        List<ElectronicFence> list1 = iFencePointsService.queryList(electronicFence);
        return AjaxResult.success(list1);
    }

    /**
     * 根据相应的区域查询相应的区域信息
     */
    //@PreAuthorize(hasPermi = "${integration:gaode}:list")
    /*@GetMapping("/getDistrictByCity")
    public AjaxResult getGaodeDistrict() {
        return  AjaxResult.success(iFencePointsService.getDistrictInfo(fence,"getDistrictInfo"));
    }*/

    /**
     * 校验围栏名称是否重复
     */
    //@PreAuthorize(hasPermi = "${integration:gaode}:add" )
    @Log(title = "围栏坐标" , businessType = BusinessType.OTHER)
    @GetMapping("/checkFence")
    public AjaxResult checkFence(ElectronicFence electronicFence) {
        if(iFencePointsService.checkFenceName(electronicFence)){
            return AjaxResult.error("手工围栏名称不能重复或该区域围栏已存在!");
        }
        return AjaxResult.success();
    }

    /**
     * 保存围栏
     */
    //@PreAuthorize(hasPermi = "${integration:gaode}:add" )
    @Log(title = "围栏坐标" , businessType = BusinessType.INSERT)
    @PostMapping("/saveCache")
    public AjaxResult addFence(@RequestBody ElectronicFence fence) {
        //取出缓存坐标并保存
        return iFencePointsService.saveOrUpdateFence(fence);
    }

    /**
     * 直接保存围栏
     */
    //@PreAuthorize(hasPermi = "${integration:gaode}:add" )
    @Log(title = "围栏坐标" , businessType = BusinessType.INSERT)
    @PostMapping("/saveFenceByQy")
    public AjaxResult addFenceByQy(@RequestBody ElectronicFence fence) {
        //取出缓存坐标并保存
        return iFencePointsService.saveFenceByQy(fence);
    }

    /**
     * 修改本地围栏信息
     */
    //@PreAuthorize(hasPermi = "${integration:gaode}:edit" )
    @Log(title = "围栏坐标" , businessType = BusinessType.UPDATE)
    @PutMapping("/upDataFenceName")
    public AjaxResult editFence(@RequestBody ElectronicFence fence) {
        return toAjax(iFencePointsService.updateByCode(fence));
    }

    /**
     * 删除电子围栏,
     */
    //@PreAuthorize(hasPermi = "${integration:gaode}:remove" )
    @Log(title = "电子围栏" , businessType = BusinessType.DELETE)
    @PostMapping("/delFence" )
    public AjaxResult remove(@RequestBody Map<String,Object> requestMap) {
        return toAjax(iFencePointsService.removeByCode(requestMap.get("code").toString(),"deleteFenceNew") ? 1 : 0);
    }

    /**
     * 生成围栏坐标,
     */
    //@PreAuthorize(hasPermi = "${integration:fence}:remove" )
    @Log(title = "电子围栏" , businessType = BusinessType.DELETE)
    @PostMapping("/genFencePoints" )
    public AjaxResult genFencePoints(@RequestBody Map<String,Object> requestMap) {
        List<List<Double>> newPaths = new ArrayList<>();
        List<List<Double>> inList = new ArrayList<>();
        List<List<Double>> outList = new ArrayList();
        List<List<Double>> paths = (List<List<Double>>) requestMap.get("paths");
        List<List<Double>> districtPaths = (List<List<Double>>) requestMap.get("districtPaths");
        for (int i = 0; i < paths.size(); i++) {
            if (districtPaths.contains(paths.get(i))) {
                inList.add(paths.get(i));
            }else{
                outList.add(paths.get(i));
            }
        }
        if(inList.size()!=3){
            return AjaxResult.error(ErroEnums.SYSERRO.getCode(),"请检查，落在区域上的点少于或超过3个点，请重新绘制");
        }
        int start = -1;
        int second = -1;
        int thrid = -1;
        for(int i=0;i<districtPaths.size();i++){
            //第三个点
            if(ListUtils.isEqualList(districtPaths.get(i),inList.get(0))){
                start = i;
            }
            if(ListUtils.isEqualList(districtPaths.get(i),inList.get(1))){
                second = i;
            }
            if(ListUtils.isEqualList(districtPaths.get(i),inList.get(2))){
                thrid = i;
            }
        }
        if(second>thrid&&thrid<start){
            List<List<Double>> list1 = SplitList.naiveSplit(districtPaths,0,thrid);
            List<List<Double>> list2 = SplitList.naiveSplit(districtPaths,thrid,start);
            List<List<Double>> list3 = SplitList.naiveSplit(districtPaths,second,districtPaths.size()-1);
            newPaths.addAll(list3);
            newPaths.add(districtPaths.get(districtPaths.size()-1));
            newPaths.addAll(list1);
            newPaths.addAll(list2);
            newPaths.add(inList.get(0));
        }else if(second<thrid&&thrid<start){
            List<List<Double>> list1 = SplitList.naiveSplit(districtPaths,second,thrid);
            List<List<Double>> list2 = SplitList.naiveSplit(districtPaths,thrid,start);
            newPaths.addAll(list1);
            newPaths.addAll(list2);
            newPaths.add(inList.get(0));
        }else if(second<thrid&&thrid>start){
            List<List<Double>> list2 = SplitList.naiveSplit(districtPaths,second,thrid);
            List<List<Double>> list3 = SplitList.naiveSplit(districtPaths,thrid,districtPaths.size()-1);
            List<List<Double>> list1 = SplitList.naiveSplit(districtPaths,0,start);
            newPaths.addAll(list2);
            newPaths.add(districtPaths.get(districtPaths.size()-1));
            newPaths.addAll(list3);
            newPaths.addAll(list1);
            newPaths.add(inList.get(0));
        }
        newPaths.addAll(outList);
        return AjaxResult.success(newPaths);
    }

}
